package com.bili.bili_data_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bili.bili_data_api.dao.mapper.RichangAreaMapper;
import com.bili.bili_data_api.dao.pojo.RichangArea;
import com.bili.bili_data_api.service.ShengHuoService;
import com.bili.bili_data_api.util.Result;
import com.bili.bili_data_api.vo.QueryVo;
import com.bili.bili_data_api.vo.ShengHuoList;
import com.bili.bili_data_api.vo.ShengHuoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShengHuoServiceImpl implements ShengHuoService {

    @Autowired
    private RichangAreaMapper richangAreaMapper;

    @Override
    public Result edit(ShengHuoList shengHuoVo) {
        RichangArea richangArea = new RichangArea();
        richangArea.setId(shengHuoVo.getId());
        richangArea.setAuthor(shengHuoVo.getAuthor());
        richangArea.setTitle(shengHuoVo.getTitle());
        richangArea.setView(shengHuoVo.getView());
        richangArea.setFavour(shengHuoVo.getFavour());
        richangAreaMapper.updateById(richangArea);

        return Result.ok();
    }

    @Override
    public Result delete(ShengHuoList shengHuoVo) {
        richangAreaMapper.deleteById(shengHuoVo.getId());
        return Result.ok();
    }

    @Override
    public Result add(RichangArea shengHuoVo) {
        richangAreaMapper.insert(shengHuoVo);
        return Result.ok();
    }

    @Override
    public ShengHuoVo selectByTitle(QueryVo query) {
        ShengHuoVo shengHuoVo = new ShengHuoVo();
        LambdaQueryWrapper<RichangArea> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(RichangArea::getTitle,query.getTitle());
        Page<RichangArea> page = new Page<>(query.getPageIndex(),query.getPageSize());
        Page<RichangArea> richangAreaPage = richangAreaMapper.selectPage(page, lambdaQueryWrapper);
        Integer integer = richangAreaMapper.selectCount(lambdaQueryWrapper);
        List<ShengHuoList> shengHuoLists = new ArrayList<>();
        for (RichangArea richangArea : richangAreaPage.getRecords()) {
            ShengHuoList shengHuoList = new ShengHuoList();
            shengHuoList.setId(richangArea.getId());
            shengHuoList.setTitle(richangArea.getTitle());
            shengHuoList.setAuthor(richangArea.getAuthor());
            shengHuoList.setView(richangArea.getView());
            shengHuoList.setFavour(richangArea.getFavour());
            shengHuoLists.add(shengHuoList);
        }
        shengHuoVo.setShengList(shengHuoLists);
        shengHuoVo.setTotal(integer);
        return shengHuoVo;
    }
}
